<?php
/**
 * Description of UserMapper
 *
 /*
 * @author     Thibaut Cromphaut and Dimitry Dierickx
 * @copyright  Copyright (c) 2011 Safti-Kado
 */
class Application_Model_PromotionMapper extends Ahs_Model_Mapper
{

    public function __construct(array $options = null)
    {
        parent::__construct($options);
        $this->DbTableName = 'Application_Model_DbTable_Promotions';
    }

    /**
     * @param integer $id
     * @return Application_Model_Promotion
     */
    public function fetchById($id)
    {
        $row = $this->getDbTable()->fetchRow(array('promotion_id = ?' => $id));

        //return $this->_toPromotion($row);
        return $row;
    }

    /**
     * @return array
     */
    public function fetchAll()
    {        
        $rows = $this->getDbTable()->fetchAll();
        
        return $this->_toPromotions($rows);
    }

    /**
     * @param Application_Model_Promotion $promotion
     */
    public function save(Application_Model_Promotion $promotion)
    {
        $data = array(
            'promotion_title'       => $promotion->Title,
            'promotion_description' => $promotion->Description,
            'promotion_content'     => $promotion->Content,
        );

        if (null == $promotion->Id) {
            $this->getDbTable()->insert($data);
        } else {
            $this->getDbTable()->update($data, array('promotion_id = ?' => $promotion->Id));
        }
    }
    
    /**
     * @param Zend_Db_Table_Row_Abstract $row
     * @return Application_Model_Promotion 
     */
    private function _toPromotion(Zend_Db_Table_Row_Abstract $row = null)
    {
        $promotion = new Application_Model_Promotion();

        
        if ($row) {
            $promotion->Id           = $row['promotion_id'];
            $promotion->Title        = $row['promotion_title'];
            $promotion->Description  = $row['promotion_description'];
            $promotion->Content      = $row['promotion_content'];
            $promotion->PriceOld     = $row['promotion_price_old'];
            $promotion->PriceNew     = $row['promotion_price_new'];            
        }
        
        return $promotion;
    }

    /**
     * @param Zend_Db_Table_Rowset_Abstract $rows
     * @return array
     */
    private function _toPromotions(Zend_Db_Table_Rowset_Abstract $rows = null)
    {
        $promotions = array();

        if ($rows) {
            foreach ($rows as $row) {
                $promotions[] = $this->_toPromotion($row);
            }
        }
        
        return $promotions;
    }

}